/*************************************************************************
> File Name: client.c
> Author: High~兵 
> Mail: QQ:1398055460 
> Created Time: 2019年08月06日 星期二 19时18分28秒
学生考试管理系统
************************************************************************/

#include<stdio.h>
#include <string.h>
#include <mysql.h>
#include <time.h>

int main(int argc , char argv[])
{
    MYSQL db , * pdb;//mysql 变量
    pdb = mysql_init(&db);//初始化数据库连接
    if(NULL == pdb)
    {
        perror("mysql init");
        return -1;
    }
    char *host = "172.16.3.145";//主机
    char *user = "root";//用户名
    char *password = "123456";//密码
    char * mydb = "test";//数据库名 
    system("clear");
    printf("正在载入数据库，请耐心等待.........\n");
    pdb = mysql_real_connect(&db,host,user,password,mydb,0,0,0);//连接数据库
    if(NULL == pdb)
    {
        printf("connect error user\n");
        return -1;
    }
    printf("远程服务器连接成功!\n");

    mysql_set_character_set( & db, "utf8");//汉字显示

    char anw[32] = {0};//答案
    int gread = 0;
    char real[256] = {0};//标准答案
    char * sql ;//sql语句
    char buff[256] = {0};
    system("clear");//清屏
    for(int i = 0;i < 3;i++)
    {
        printf("*************************************************************\n");
        printf("****1.登陆已有帐号  2.注册新的帐号 3.教师登陆  0.退出系统****\n");
        printf("*************************************************************\n");
        int chose = 0;
        printf("test-system--->");
        int ret = scanf("%d",&chose);
        if(ret != 1)
        {
            printf("输入错误！\n");
            return 0;
        } 
        char name[32] = {0};
        char pass[32] = {0};
        switch(chose)
        {
            case 3:
            { 
                printf("请输入用户名:");
                getchar();
                scanf("%s",name);
                printf("请输入密码:");
                getchar();
                scanf("%s",pass);
                getchar();
                sprintf(buff,"select * from   root  where name = '%s' and pass = '%s';",name,pass);
                sql = buff;
                int ret = mysql_real_query(&db,sql,strlen(sql));//执行sql语句
                if(ret < 0 || mysql_errno(&db))
                {
                    printf("ERROR = %s\n",mysql_error(&db));
                    return -1;
                } 
                MYSQL_RES * result = NULL;
                result = mysql_store_result(&db);
                if(NULL == result)
                {
                    perror("mysql_store_result error");
                    return -1;
                }
                int row = mysql_num_rows(result);
                printf("%d\n",row);
                if( row <=  0)//登陆失败
                {
                    printf("用户名或密码错误！\n");
                    break;
                }
                system("clear");
                printf("登陆成功！\n");
                while(1)
                {
                    printf("*******************************************************************\n");
                    printf("*******1.查看题库  2.增加题  3.删除题  4.查看考试记录 0.退出*******\n");
                    int chose2 = 0;
                    printf("请输入：");
                    int ret2 = scanf("%d",&chose2);
                    if(ret2 != 1)
                    {
                        scanf("%*s");
                        printf("输入有误！\n");
                        continue;
                    }
                    if(chose2 == 4)
                    {
                        sprintf(buff,"select * from  login  ;");//考试记录表内容
                        sql = buff;
                        int ret1 = mysql_real_query(&db,sql,strlen(sql));//考试记录
                        if(ret1 < 0)
                        {
                            perror("query error");
                            return -1;
                        }
                        if(mysql_errno(&db))
                        {
                            printf("error = %s\n",mysql_error(&db));
                            return -1;
                        }
                        MYSQL_RES * result = NULL;//考试记录结果集
                        result = mysql_store_result(&db);
                        if(NULL == result)
                        {
                            perror("mysql_store_result error");
                            return -1;
                        }
                        MYSQL_ROW row;
                        for(int i = 0;i <mysql_num_rows(result);i++)
                        {
                            row = mysql_fetch_row(result);
                            if(row <= 0)
                            break;
                            printf(" %s %s %s\n",row[1],row[2],row[3]);

                        }
                    }
                    if(chose2 == 1)//查看题库
                    {
                        sprintf(buff,"select * from  question ;");//question表内容
                        sql = buff;
                        int ret1 = mysql_real_query(&db,sql,strlen(sql));//问题库
                        if(ret1 < 0)
                        {
                            perror("query error");
                            return -1;
                        }
                        if(mysql_errno(&db))
                        {
                            printf("error = %s\n",mysql_error(&db));
                            return -1;
                        }
                        MYSQL_RES * result = NULL;//题库结果集
                        result = mysql_store_result(&db);
                        if(NULL == result)
                        {
                            perror("mysql_store_result error");
                            return -1;
                        }
                        MYSQL_ROW row;
                        for(int i = 0;i <mysql_num_rows(result);i++)
                        {
                            row = mysql_fetch_row(result);
                            if(row <= 0)
                            break;
                            printf("%s. %s \n %s\n 正确答案：%s\n",row[0],row[2],row[3],row[1]);

                        } 
                    }
                    if(chose2 == 2)//增加
                    {
                        char real[32] = {0};
                        char que[256] = {0};
                        char cho1[128]= {0};
                        char cho2[128] = {0};
                        char cho3[128] = {0};
                        char cho4[128] = {0};
                        printf("请输入问题：");
                        scanf("%s",que);
                        getchar();
                        printf("请输入选项1：");
                        scanf("%s",cho1);
                        getchar();
                        printf("请输入选项2：");
                        scanf("%s",cho2);
                        getchar();
                        printf("请输入选项3：");
                        scanf("%s",cho3);
                        getchar();
                        printf("请输入选项4：");
                        scanf("%s",cho4);
                        getchar();
                        printf("请输入正确选项：");
                        scanf("%s",real);
                        getchar();
                        sprintf(buff,"insert into question values(NULL,'%s','%s','%s %s %s %s');",real,que,cho1,cho2,cho3,cho4);
                        sql = buff;
                        int ret4 = mysql_real_query(&db,sql,strlen(sql));
                        printf("插入成功！\n");
                    }
if(chose2 == 3)//删除题
{
    int id1 =  0;
    printf("请输入你要删除的题号：");
    scanf("%d",&id1);
    sprintf(buff,"delete from  question where no = %d;",id1);
    sql = buff;
    int ret3 = mysql_real_query(&db,sql,strlen(sql));
    printf("删除成功！\n");
}
if(chose2 == 0)
{
    system("clear");
    return 0;
    scanf("%*s");
    break;
}
}
}
case 0://退出系统 
{
    system("clear");
    printf("====>by!<====\n");
    return 0;
}
case 1://登陆帐号
{
    printf("请输入用户名:");
    getchar();
    scanf("%s",name);
    printf("请输入密码:");
    getchar();
    scanf("%s",pass);
    getchar();
    sprintf(buff,"select * from  user where name = '%s' and pass = '%s';",name,pass);
    sql = buff;
    int ret = mysql_real_query(&db,sql,strlen(sql));//执行sql语句
    if(ret < 0 || mysql_errno(&db))
    {
        printf("ERROR = %s\n",mysql_error(&db));
        return -1;
    } 
    MYSQL_RES * result = NULL;
    result = mysql_store_result(&db);
    if(NULL == result)
    {
        perror("mysql_store_result error");
        return -1;
    }
    int row = mysql_num_rows(result);
    printf("%d\n",row);
    if( row <=  0)//登陆失败
    {
        printf("用户名或密码错误！\n");
        break;
    }
    system("clear");
    printf("登陆成功！\n");
    while(1)
    {
        printf("****1.开始答题 2.修改密码 3.返回 4.考试记录****\n");
        int chose1 = 0;
        printf("input-->");
        scanf("%d",&chose1);
        if(chose1 == 4)
        {
            printf("请输入你的用户名");
            scanf("%s",name);
            sprintf(buff,"select * from  login where name = '%s';",name);//question表内容
            sql = buff;
            int ret1 = mysql_real_query(&db,sql,strlen(sql));//日志记录
            if(ret1 < 0)
            {
                perror("query error");
                return -1;
            }
            if(mysql_errno(&db))
            {
                printf("error = %s\n",mysql_error(&db));
                return -1;
            }
            MYSQL_RES * result = NULL;
            result = mysql_store_result(&db);
            if(NULL == result)
            {
                perror("mysql_store_result error");
                return -1;
            }
            MYSQL_ROW row;
            for(int k = 0;k < mysql_num_rows(result);k++)
            {
                row = mysql_fetch_row(result);
                if(row <= 0)
                break;
                printf("%s  %s  %s %s\n",row[0],row[1],row[2],row[3]);
            }
        }
        if(chose1 == 3)
        {
            i = 0;
            break;
        }
        if(chose1 == 2)//修改密码
        {
            printf("请输入你的新密码：");
            scanf("%s",pass);
            sprintf(buff,"update user set pass = '%s' where name = '%s';",pass,name);
            sql = buff; 
            int ret = mysql_real_query(&db,sql,strlen(sql));//执行sql语句
            if(ret < 0 | mysql_errno(&db))
            {
                printf("ERROR = %s\n",mysql_error(&db));
                break;
            } 
            system("clear");
            printf("密码修改成功！\n");
            i = 0;
            break ;
        }
if(chose1 == 1)//开始答题
{   
    sprintf(buff,"select * from  question ;");//question表内容
    sql = buff;
    int ret1 = mysql_real_query(&db,sql,strlen(sql));//问题库
    if(ret1 < 0)
    {
        perror("query error");
        return -1;
    }
    if(mysql_errno(&db))
    {
        printf("error = %s\n",mysql_error(&db));
        return -1;
    }
    MYSQL_RES * result = NULL;//题库结果集
    result = mysql_store_result(&db);
    if(NULL == result)
    {
        perror("mysql_store_result error");
        return -1;
    }
    int row = mysql_num_rows(result);//总题数
    printf("温馨提醒：本次考试共10题，每题10分，满分100分，请认真答题！\n");
    for(int j = 0; j < 10;j++)
    {
        printf("\n");
        int tmp = rand()%row;//产生随机题号
        bzero(buff,sizeof(buff));
        sprintf(buff,"select * from question where no = '%d';",tmp);
        sql = buff;
        mysql_real_query(&db,sql,strlen(sql)); 
        if(mysql_errno(&db))
        {
            printf("error = %s\n",mysql_error(&db));
            return -1;
        }
        MYSQL_RES * result = NULL;//结果集行数
        result = mysql_store_result(&db);
        if(NULL == result)
        {
            printf("error\n");
            return -1;
        }
        MYSQL_ROW row;
        row = mysql_fetch_row(result);
        printf("%d. %s  \n %s \n",j+1,row[2],row[3]);//题
        printf("请输入你的选择(答案为大写字母请注意):");
        while(1)
        {
            scanf("%s",anw);
            getchar();
            if(!strcmp(anw,"A") || !strcmp(anw,"B") || !strcmp(anw,"C") || !strcmp(anw,"D"))//判断输入选项是否正确
            break;
            printf("输入选项有误请重新输入(大写字母)：");
        }
        if(!strcmp(anw,row[1]))
        {
            gread = gread + 20;
            printf("*****>回答正确！\n");
        }
        else
        {
            printf("\n回答错误！正确答案：%s\n",row[1]);
        }
    }
int ok  = 1 ;
printf("\n");
printf("=========>答题结束是否提交<=========\n******1.提交答案 0.返回*******\n");
while(1)
{

    int ret = scanf("%d",&ok);
    if(ret != 1)
    {
        scanf("%*s");
        printf("输入有误!\n");
        continue;
    }

    if(ok == 0)break;
    if(ok == 1)
    {
        system("clear");
        printf("*********************************************************\n                    你的成绩：%d\n*********************************************************\n",gread);
        bzero(buff,sizeof(buff));
        struct tm * p;
        time_t tt;
        time_t ts;
        struct tm tr = {0};
        time(&tt);
        p = localtime(&tt);
        char time[128] = {0};
        sprintf(time,"%4d/%02d/%02d/%02d.%02d.%02d",p->tm_year + 1900,p->tm_mon + 1,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
        sprintf(buff,"insert into login values (NULL,'%s','%s',%d);",time,name,gread);//考试成绩记录
        int ret = mysql_real_query(&db,sql,strlen(sql));//执行sql语句
        if(ret < 0 | mysql_errno(&db))
        {
            printf("ERROR = %s\n",mysql_error(&db));
            return -1;
        }
        gread = 0;
        break;
    }
}
}
}
break;
            }
            case 2://注册帐号
            {
                getchar();
                printf("请输入用户名:");
                scanf("%s",name);
                printf("请输入密码:");
                scanf("%s",pass);

                sprintf(buff,"select * from user where name = '%s';",name);//查找user表是否有同名用户
                sql = buff;
                int ret = mysql_real_query(&db,sql,strlen(sql));//user表查找同名用户 
                if(ret < 0 | mysql_errno(&db))
                {
                    printf("ERROR = %s\n",mysql_error(&db));
                    break;
                } 
                MYSQL_RES * result = NULL;
                result = mysql_store_result(&db);
                if(NULL == result)
                {
                    perror("mysql_store_result error");
                    break;
                }
                int row = mysql_num_rows(result);//结果集行数
                if( row > 0 )
                {
                    printf("该用户已存在，请重新注册\n");
                    i--;
                    break;
                }
                sprintf(buff,"insert into user values(NULL,'%s','%s',NULL);",name,pass);//插入该用户到user表的sql语句
                sql = buff; 
                ret = mysql_real_query(&db,sql,strlen(sql));//执行sql语句插入该用户到user表
                if(ret < 0 | mysql_errno(&db))
                {
                    printf("ERROR = %s\n",mysql_error(&db));
                    break;
                }
                printf("用户注册成功，请登陆!\n");//注册成功
                i = i - 1;
                break;
            }
            default :
                {
                    printf("input error\n");
                }


        }
        }
        return 0;
}

